﻿using MvcSmartTool.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvcSmartTool.ServiceGenerator {
    public class ServiceProjectWriter : BaseProjectWriter {
        private static Guid s_projectGuid;
        public override string ProjectName {
            get { return SolutionName + ".Service"; }
        }
        public ServiceProjectWriter(string solutionName)
            : base(solutionName) {

        }
        public ServiceProjectWriter(string projectName, string projectOutput)
            : base(projectName) {
            OutputPath = projectOutput;
        }
        protected override void InternalGenerate() {

            GenerateRepository();
            GenerateService();
            GenerateUnitOfWork();
            GenerateDbContext();

            var template = new ServiceProjectTemplate() {
                Model = Models,
                SolutionName = SolutionName
            };
            FileWriterHelper.WriteFile(ProjectPath, ProjectName + ".csproj", template.TransformText());
            // Generate Assembly Info
            var ass = new AssemblyInfoTemplate {
                SolutionName = SolutionName
            };
            FileWriterHelper.WriteFile(Path.Combine(ProjectPath, "Properties"), "AssemblyInfo.cs", ass.TransformText());
        }
        private void GenerateRepository() {
            var directory = Path.Combine(ProjectPath, "Repositories");
            CopyTemplate("UserRepository", directory, SolutionName);
            foreach (var model in Models) {
                var modelTemplate = new RepositoryTemplate() {
                    Model = model,
                    SolutionName = SolutionName
                };
                FileWriterHelper.WriteFile(directory, model.Name + "Repository.cs", modelTemplate.TransformText());
            }
        }
        private void GenerateService() {
            var directory = Path.Combine(ProjectPath, "Services");
            //Copy all templates
            CopyTemplate("BaseService", directory, SolutionName);
            CopyTemplate("UserService", directory, SolutionName);
            //
            foreach (var model in Models) {
                var template = new ServiceTemplate {
                    Model = model,
                    SolutionName = SolutionName
                };
                FileWriterHelper.WriteFile(directory, model.Name + "Service.cs", template.TransformText());
            }
        }
        private void GenerateUnitOfWork() {
            var template = new UnitOfWorkTemplate();
            template.Model = Models;
            template.SolutionName = SolutionName;
            FileWriterHelper.WriteFile(ProjectPath, SolutionName + "UnitOfWork.cs", template.TransformText());
        }
        private void GenerateDbContext() {
            var template = new DbContextTemplate();

            template.Model = Models;
            template.SolutionName = SolutionName;
            FileWriterHelper.WriteFile(ProjectPath, SolutionName + "Context.cs", template.TransformText());
        }
        public static Guid ProjectGuid {
            get {
                if (s_projectGuid == Guid.Empty) {
                    s_projectGuid = Guid.NewGuid();
                }
                return s_projectGuid;
            }
        }
    }
}
